All files / src/app/api/support/articles/[slug]/helpful route.ts

0% Statements 0/72
100% Branches 0/0
0% Functions 0/1
0% Lines 0/72

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73                                                                                                                                                 
export const dynamic = "force-dynamic";

/**
 * Article Helpful Vote API
 * POST /api/support/articles/[slug]/helpful - Vote article as helpful/not helpful
 */

import { NextRequest, NextResponse } from 'next/server';
import { prisma } from "@/lib/prisma";
import { ArticleHelpfulSchema } from "@/lib/validation/support-schemas";
import { recordArticleVote } from "@/lib/support/chatbot-utils";
import { logger } from "@/lib/logging";
import {
  withErrorHandling,
  successResponse,
  ApiError,
  ApiSuccessResponse } from "@/lib/api";
import { RouteContext } from "@/lib/api/middleware";

interface HelpfulResponse {
  message: string;
}

/**
 * POST /api/support/articles/[slug]/helpful
 * Vote article as helpful/not helpful (public)
 */
async function handlePost(
  request: NextRequest,
  context?: RouteContext
): Promise<NextResponse<ApiSuccessResponse<HelpfulResponse>>> {
  if (!context?.params) {
    throw ApiError.validation("Article slug is required");
  }

  const { slug } = await context.params;

  // Find the article
  const article = await prisma.supportArticle.findUnique({
    where: { slug },
    select: { id: true, isPublished: true }});

  // Only return published articles for public API
  if (!article || !article.isPublished) {
    throw ApiError.notFound("Article", slug);
  }

  // Validate input
  const body = await request.json();
  const validationResult = ArticleHelpfulSchema.safeParse(body);
  if (!validationResult.success) {
    throw ApiError.validation(
      "Validation failed",
      validationResult.error.flatten().fieldErrors
    );
  }

  const { helpful } = validationResult.data;

  // Record the vote
  await recordArticleVote(article.id, helpful);

  logger.info("Article helpfulness vote recorded", {
    category: "API",
    articleId: article.id,
    slug,
    helpful});

  return successResponse({ message: "Thank you for your feedback!" });
}

export const POST = withErrorHandling(handlePost);